package com.zhangyu.eat.login.view;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.LogInCallback;
import com.avos.sns.*;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.zhangyu.eat.R;
import com.zhangyu.eat.base.BaseActivity;
import com.zhangyu.eat.bomain.FirstActivity;
import com.zhangyu.eat.main.view.MainActivity;
import com.zhangyu.eat.register.view.RegisterActivity;
import com.zhangyu.eat.sql.UserDao;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoaginActivity extends BaseActivity {


    @BindView(R.id.main_wx)
    TextView mainWx;
    @BindView(R.id.frist_image)
    ImageView fristImage;
    @BindView(R.id.login_linear)
    LinearLayout loginLinear;
    @BindView(R.id.login_phone)
    TextView loginPhone;
    @BindView(R.id.activity_loagin)
    RelativeLayout activityLoagin;

    @Override
    protected void initView() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(3000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        fristImage.setVisibility(View.GONE);
                    }
                });
            }
        }).start();
        AVUser currentUser = AVUser.getCurrentUser();
        if (currentUser != null) {
            Intent intent = new Intent(LoaginActivity.this, FirstActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
            // 跳转到首页
        } else {
            //缓存用户对象为空时，可打开用户注册界面…
        }
        loginPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoaginActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });



        mainWx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    SNS.setupPlatform(SNSType.AVOSCloudSNSSinaWeibo, "https://leancloud.cn/1.1/sns/goto/u2wf5n3nqosczpvt");
                    SNS.loginWithCallback(LoaginActivity.this, SNSType.AVOSCloudSNSSinaWeibo, new SNSCallback() {
                        @Override
                        public void done(SNSBase base, SNSException e) {
                            if (e == null) {
                                SNS.loginWithAuthData(base.userInfo(), new LogInCallback<AVUser>() {
                                    @Override
                                    public void done(final AVUser user, AVException e) {
                                    }
                                });
                            }
                        }
                    });

                } catch (AVException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_loagin;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}
